package jp.ac.u_tokyo.dch.util.common;

import java.io.File;

import jp.ac.u_tokyo.dch.model.xml.DisplayDefine;
import jp.ac.u_tokyo.dch.util.XmlCommonUtil;
import jp.ac.u_tokyo.dch.view.Constants;

import com.neocore.httpclient.SessionManagedNeoConnection;

/**
 * @version 1.1
 */
public class DisplayUtil {


	private final static String SELECT_ID = "/ND/def-items/def-item[@sort=\"%s\"]";

	private static DisplayDefine define1;
	private static DisplayDefine define2;
	private static DisplayDefine define3;
	private static DisplayDefine define4;
	private static DisplayDefine define5;
	private static DisplayDefine define6;
	private static DisplayDefine define7;

	//private Logger logger = Logger.getLogger(getClass());

	static {

		String defPath1 = DisplayUtil.class.getClassLoader().getResource("resource/def_001.xml").getPath();
		String defPath2 = DisplayUtil.class.getClassLoader().getResource("resource/def_002.xml").getPath();
		String defPath3 = DisplayUtil.class.getClassLoader().getResource("resource/def_003.xml").getPath();
		String defPath4 = DisplayUtil.class.getClassLoader().getResource("resource/def_004.xml").getPath();
		String defPath5 = DisplayUtil.class.getClassLoader().getResource("resource/def_005.xml").getPath();
		String defPath6 = DisplayUtil.class.getClassLoader().getResource("resource/def_006.xml").getPath();
		String defPath7 = DisplayUtil.class.getClassLoader().getResource("resource/def_007.xml").getPath();



		define1 = XmlCommonUtil.xmlFileToObject(new File(defPath1), DisplayDefine.class);
		define2 = XmlCommonUtil.xmlFileToObject(new File(defPath2), DisplayDefine.class);
		define3 = XmlCommonUtil.xmlFileToObject(new File(defPath3), DisplayDefine.class);
		define4 = XmlCommonUtil.xmlFileToObject(new File(defPath4), DisplayDefine.class);
		define5 = XmlCommonUtil.xmlFileToObject(new File(defPath5), DisplayDefine.class);
		define6 = XmlCommonUtil.xmlFileToObject(new File(defPath6), DisplayDefine.class);
		define7 = XmlCommonUtil.xmlFileToObject(new File(defPath7), DisplayDefine.class);

		//ActionSupport as = new ActionSupport();
//		File file1 = new File(as.getText("defXmlDir"), "def_lv1.xml");
//		File file2 = new File(as.getText("defXmlDir"), "def_lv2.xml");
//		File file3 = new File(as.getText("defXmlDir"), "def_lv3.xml");
//		File file4 = new File(as.getText("defXmlDir"), "def_lv4.xml");
//		File file5 = new File(as.getText("defXmlDir"), "def_lv5.xml");
//		File file6 = new File(as.getText("defXmlDir"), "def_lv6.xml");
//		define1 = getItemDefineByXML(OutputContants.LV1_SORT);
//		define2 = getItemDefineByXML(OutputContants.LV2_SORT);
//		define3 = getItemDefineByXML(OutputContants.LV3_SORT);
//		define4 = getItemDefineByXML(OutputContants.LV4_SORT);
//		define5 = getItemDefineByXML(OutputContants.LV5_SORT);
//		define6 = getItemDefineByXML(OutputContants.LV6_SORT);
//		define7 = getItemDefineByXML(OutputContants.LV7_SORT);
	}

	/**
	 * 画面定義を取得する
	 * @param sort 分類
	 * @return 画面定義オブジェクト
	 */
	public static DisplayDefine getItemDefine(String sort) {

		if (Constants.LV1_SORT.equals(sort)) {
			return define1;
		} else if (Constants.LV2_SORT.equals(sort)) {
			return define2;
		} else if (Constants.LV3_SORT.equals(sort)) {
			return define3;
		} else if (Constants.LV4_SORT.equals(sort)) {
			return define4;
		} else if (Constants.LV5_SORT.equals(sort)) {
			return define5;
		} else if (Constants.LV6_SORT.equals(sort)) {
			return define6;
		} else if (Constants.LV7_SORT.equals(sort)) {
			return define7;
		}

		return null;
	}

	/**
	 * DBから画面定義オブジェクトを取得する
	 * @param sort 分類
	 * @return 画面定義オブジェクト
	 */
	private static DisplayDefine getItemDefineByXML(String sort) {
		String result = null;
		SessionManagedNeoConnection session = null;
		try {
			//session  = new  SessionManagedNeoConnection("10.91.11.13", 7700);
			//session.login("cooba", "cooba");
			//session = SessionManager.getsesion();
			result = session.queryXML(String.format(SELECT_ID, sort));
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			try {
				session.logout();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return XmlCommonUtil.xmlToObject(XmlCommonUtil.getXmlContents(result), DisplayDefine.class);
	}

}
